package data_report

import (
	"context"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CapitalChangeReportLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 资金账变
func NewCapitalChangeReportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CapitalChangeReportLogic {
	return &CapitalChangeReportLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CapitalChangeReportLogic) CapitalChangeReport(req *types.CapitalChangeReportRequest) (resp *types.CapitalChangeReportItemResponse, err error) {
	resp = &types.CapitalChangeReportItemResponse{
		TotalScore:  0,
		AmountScore: 0,
		List:        make([]types.CapitalChangeReportItem, 0),
		Total:       0,
		Page:        0,
		PageSize:    0,
	}
	logrep := &rpc.UserWalletLogRequest{
		Page:      int64(req.Page),
		PageSize:  int64(req.PageSize),
		UserId:    req.UserId,
		TypeId:    req.TypeId,
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		OperateId: req.OperateUserId,
		Id:        req.OrderId,
	}
	logList, err := l.svcCtx.AssetsRpc.User.UserWalletLog(l.ctx, logrep)
	if err != nil {
		return nil, err
	}
	if len(logList.Data) == 0 {
		return resp, nil
	}

	for _, log := range logList.Data {
		resp.List = append(resp.List, types.CapitalChangeReportItem{
			OrderId:      log.Id,
			UserId:       log.UserId,
			TypeId:       log.TypeId,
			TypeName:     log.TypeName,
			BalanceStart: log.BalanceStart,
			BalanceEnd:   log.BalanceEnd,
			Amount:       log.Amount,
			Description:  log.Description,
			CreatedAt:    log.CreatedAt,
			OperatorId:   log.OperateId,
			OperatorName: strconv.FormatInt(log.OperateId, 10),
		})
	}
	resp.Total = int32(logList.Meta.Total)
	resp.Page = int32(logList.Meta.Page)
	resp.Page = int32(logList.Meta.PageSize)

	return
}
